﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebMainSite
{
    public partial class ManageUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //显示个人信息
                InitPersonInfo();
                //控制admin权限的div模块显不显示
                ControlDivVisible();
            }
        }
        /// <summary>
        /// 初始化加载GridView数据
        /// </summary>
        private void InitDataGridView()
        {
            MySite.BLL.UserInfo userInfoBll = new MySite.BLL.UserInfo();
            //数据库存的该系统默认超级管理是admin，不可修改，不可删除，所以也不查询出来
            DataSet ds = userInfoBll.GetList("UserId!='admin'");
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "UserID" };//主键
            GridView1.DataBind();
            
        }

        protected void BtAddUser_Click(object sender, EventArgs e)
        {
            LbMessage.Text = "";

            MySite.BLL.UserInfo userInfoBll = new MySite.BLL.UserInfo();
            bool isExists = userInfoBll.Exists(userid.Value.Trim());
            if (isExists)
            {
                LbMessage.Text = "<div class='box box-error'>已存在用户:" + userid.Value + "</div>";
                return;
            }
            MySite.Model.UserInfo userInfo = new MySite.Model.UserInfo();
            userInfo.UserID = userid.Value.Trim();
            userInfo.UserName = username.Value.Trim();
            userInfo.PassWord = MD5Encryption(password.Value.Trim());
            bool flag= userInfoBll.Add(userInfo);
            if (flag)
            {
                LbMessage.Text = "<div class='box box-success'>新增用户成功！</div>";
                ClearInputData();
                InitDataGridView();
            }
            else
                LbMessage.Text = "<div class='box box-error'>新增用户失败！</div>";
        }

        private void ClearInputData()
        {
            userid.Value = "";
            username.Value = "";
            password.Value = "";
            password_confirm.Value = "";
        }
        //private bool ValidataInput()
        //{
        //    if (userid.Value.Length < 6)
        //    {
        //        Response.Write("<script>alert('登录名长度不少于6个字符')</script>");
        //        return false;
        //    }
        //    else if (username.Value.Length > 10)
        //    {
        //        Response.Write("<script>alert('名字不能大多于5个汉字')</script>");
        //        return false;
        //    }
        //    else if (password.Value.Length < 6)
        //    {
        //        Response.Write("<script>alert('密码长度不少于6个字符')</script>");
        //        return false;
        //    }
        //    else if (password_confirm.Value != password.Value)
        //    {
        //        Response.Write("<script>alert('输入的确认密码不正确')</script>");
        //        return false;
        //    }
        //    return true;
        //}

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            InitDataGridView();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string userid=GridView1.DataKeys[e.RowIndex].Value.ToString();
            MySite.BLL.UserInfo userInfoBll = new MySite.BLL.UserInfo();
            userInfoBll.Delete(userid);
            InitDataGridView();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('删除操作不可逆，你确认要删除：\"" + e.Row.Cells[0].Text + "\"吗?')");
                }
            }
        }
        /// <summary>
        /// 初始化个人信息显示出来
        /// </summary>
        private void InitPersonInfo()
        {
             MySite.Model.UserInfo userInfo=Session["userInfo"] as MySite.Model.UserInfo;
             newUserid.Value = userInfo.UserID;
             newUsername.Value = userInfo.UserName;
        }
        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtChangeUserInfo_Click(object sender, EventArgs e)
        {
            MySite.Model.UserInfo userInfo = Session["userInfo"] as MySite.Model.UserInfo;
            if (MD5Encryption(OldPassword.Value.Trim()) != userInfo.PassWord)
            {
                LbChangeMessage.Text = "<div class='box box-error'>旧密码输入错误！</div>";
                return;
            }
            else
            {
                MySite.Model.UserInfo userInfoChange = new MySite.Model.UserInfo();
                MySite.BLL.UserInfo userInfoChangeBll = new MySite.BLL.UserInfo();
                userInfoChange.UserID = newUserid.Value;
                userInfoChange.PassWord = MD5Encryption(newPassword.Value.Trim());
                userInfoChange.UserName = newUsername.Value.Trim();
                try
                {
                    userInfoChangeBll.Update(userInfoChange);
                    LbChangeMessage.Text = "<div class='box box-success'>修改个人信息成功！</div>";
                }
                catch (Exception ex)
                {
                    LbChangeMessage.Text = "<div class='box box-error'>修改个人信息失败！原因如下:"+ex.Message+"</div>";
                }
            }
        }
        /// <summary>
        /// 控制两个div层的显示，如果不是admin的话，把有admin权限的两个层隐藏掉
        /// </summary>
        private void ControlDivVisible()
        {
            MySite.Model.UserInfo userInfo = Session["userInfo"] as MySite.Model.UserInfo;
            if (userInfo.UserID == "admin")
            {
                AddUserDiv.Visible = true;
                AllUserDiv.Visible = true;
                //admin用户才加载这个数据
                InitDataGridView();
            }
            else
            {
                AddUserDiv.Visible = false;
                AllUserDiv.Visible = false;
            }
        }
    }
}